D:\a\tools.proto\tools.proto\compiler\src\gen\swift\util.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::structure::{Field, FixedFieldType}; |
30 | | use crate::compiler::util::types::TypeMapper; |
31 | | use crate::compiler::Protocol; |
32 | | use crate::gen::template::util::CaseConversion; |
33 | | use crate::model::protocol::Endianness; |
34 | | use std::borrow::Cow; |
35 | | |
36 | | pub struct SwiftTypeMapper<'a> { |
37 | | proto_name: Cow<'a, str>, |
38 | | } |
39 | | |
40 | | impl<'a> SwiftTypeMapper<'a> { |
41 | 0 | pub fn from_protocol(proto: &'a Protocol) -> Self { |
42 | 0 | Self { |
43 | 0 | proto_name: proto.name().to_pascal_case(), |
44 | 0 | } |
45 | 0 | } |
46 | | } |
47 | | |
48 | | impl TypeMapper for SwiftTypeMapper<'_> { |
49 | 0 | fn map_local_type<'a>(&self, item_type: &'a str) -> Cow<'a, str> { |
50 | 0 | format!("{}{}", self.proto_name, item_type).into() |
51 | 0 | } |
52 | | |
53 | 0 | fn map_foreign_type<'a>(&self, item_type: &'a str) -> Cow<'a, str> { |
54 | 0 | item_type.into() |
55 | 0 | } |
56 | | } |
57 | | |
58 | | macro_rules! gen_value_type { |
59 | | ($prefix: literal, $ty: expr, $suffix: literal) => { |
60 | | match $ty { |
61 | | FixedFieldType::Int8 => concat!($prefix, "Int8", $suffix), |
62 | | FixedFieldType::Int16 => concat!($prefix, "Int16", $suffix), |
63 | | FixedFieldType::Int32 => concat!($prefix, "Int32", $suffix), |
64 | | FixedFieldType::Int64 => concat!($prefix, "Int64", $suffix), |
65 | | FixedFieldType::UInt8 => concat!($prefix, "UInt8", $suffix), |
66 | | FixedFieldType::UInt16 => concat!($prefix, "UInt16", $suffix), |
67 | | FixedFieldType::UInt32 => concat!($prefix, "UInt32", $suffix), |
68 | | FixedFieldType::UInt64 => concat!($prefix, "UInt64", $suffix), |
69 | | FixedFieldType::Float32 => concat!($prefix, "Float32", $suffix), |
70 | | FixedFieldType::Float64 => concat!($prefix, "Float64", $suffix), |
71 | | FixedFieldType::Bool => concat!($prefix, "Bool", $suffix), |
72 | | } |
73 | | }; |
74 | | } |
75 | | |
76 | | pub struct SwiftUtils; |
77 | | |
78 | | impl crate::gen::base::structure::Utilities for SwiftUtils { |
79 | 0 | fn get_field_type(field_type: FixedFieldType) -> &'static str { |
80 | 0 | gen_value_type!("", field_type, "") |
81 | 0 | } |
82 | | |
83 | 0 | fn get_fragment_name(field: &Field) -> &'static str { |
84 | 0 | //TODO: Check if unwrap is safe |
85 | 0 | let raw_field_type = field.ty.as_fixed().unwrap().bits_type; |
86 | 0 | let raw_field_byte_size = raw_field_type.get_byte_size(); |
87 | 0 | match raw_field_byte_size != field.loc.byte_size { |
88 | 0 | true => "unaligned", |
89 | 0 | false => "aligned", |
90 | | } |
91 | 0 | } |
92 | | |
93 | 0 | fn get_fragment_name_mut(field: &Field) -> &'static str { |
94 | 0 | //TODO: Check if unwrap is safe |
95 | 0 | let raw_field_type = field.ty.as_fixed().unwrap().bits_type; |
96 | 0 | let raw_field_byte_size = raw_field_type.get_byte_size(); |
97 | 0 | match raw_field_byte_size != field.loc.byte_size { |
98 | 0 | true => "unaligned", |
99 | 0 | false => "aligned", |
100 | | } |
101 | 0 | } |
102 | | |
103 | 0 | fn get_bit_codec_inline(endianness: Endianness) -> &'static str { |
104 | 0 | match endianness { |
105 | 0 | Endianness::Little => "BP3DProto.BitCodecLE", |
106 | 0 | Endianness::Big => "BP3DProto.BitCodecBE", |
107 | | } |
108 | 0 | } |
109 | | |
110 | 0 | fn get_byte_codec_inline(endianness: Endianness) -> &'static str { |
111 | 0 | match endianness { |
112 | 0 | Endianness::Little => "BP3DProto.ByteCodecLE", |
113 | 0 | Endianness::Big => "BP3DProto.ByteCodecBE", |
114 | | } |
115 | 0 | } |
116 | | |
117 | 0 | fn get_byte_codec(endianness: Endianness) -> &'static str { |
118 | 0 | match endianness { |
119 | 0 | Endianness::Little => "BP3DProto.ByteCodecLE", |
120 | 0 | Endianness::Big => "BP3DProto.ByteCodecBE", |
121 | | } |
122 | 0 | } |
123 | | } |
124 | | |
125 | | impl crate::gen::base::message::Utilities for SwiftUtils { |
126 | 0 | fn get_value_type(endianness: Endianness, ty: FixedFieldType) -> &'static str { |
127 | 0 | match endianness { |
128 | 0 | Endianness::Little => gen_value_type!("BP3DProto.ValueLE<B, ", ty, ">"), |
129 | 0 | Endianness::Big => gen_value_type!("BP3DProto.ValueBE<B, ", ty, ">"), |
130 | | } |
131 | 0 | } |
132 | | |
133 | 0 | fn get_value_type_inline(endianness: Endianness, ty: FixedFieldType) -> &'static str { |
134 | 0 | match endianness { |
135 | 0 | Endianness::Little => gen_value_type!("BP3DProto.ValueLE<B, ", ty, ">"), |
136 | 0 | Endianness::Big => gen_value_type!("BP3DProto.ValueBE<B, ", ty, ">"), |
137 | | } |
138 | 0 | } |
139 | | |
140 | 0 | fn gen_struct_ref_type(type_name: &str) -> String { |
141 | 0 | format!("{}<B>", type_name) |
142 | 0 | } |
143 | | |
144 | 0 | fn gen_message_ref_type(type_name: &str) -> String { |
145 | 0 | format!("{}<B>", type_name) |
146 | 0 | } |
147 | | } |